package hrms.controller;

import hrms.entity.Absenteeism;
import hrms.entity.Result;
import hrms.entity.Staff;
import hrms.entity.WorkChange;
import hrms.service.StaffService;
import hrms.service.WorkChangeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/workchange")
public class WorkChangeController {
    @Autowired
    private WorkChangeService workChangeService;
    @Autowired
    private StaffService staffService;

    @PostMapping("/getall")
    public Result showAllWorkChange(){
        Map<String,Object> modelMap = new HashMap<String,Object>();
        modelMap.put("list",workChangeService.selectAllWorkChange());
        return Result.success().addMap(modelMap);
    }

    @PostMapping("/add")
    public  Result addNewWorkChange(@RequestBody WorkChange workChange){
        Map<String,Object> map = new HashMap<String,Object>();
        workChange.setChangeTime(new Date());
        int res = workChangeService.insertWorkChange(workChange);

        if(res<0){
            return Result.fail().error("插入失败");
        }
        map.put("success","插入成功");
        return Result.success().addMap(map);
    }

    @PostMapping("/searchid/{id}")
    public Result searchById(@PathVariable("id")Integer id){
        Map<String,Object> map = new HashMap<String,Object>();
        List<WorkChange> list = workChangeService.selectWorkChangeByStaffId(id);
        if (list.size()==0){
            return Result.fail().error("请再次检查员工的id");
        }
        map.put("list",list);
        return Result.success().addMap(map);
    }

    @PostMapping("/delete/{id}")
    public  Result deleteWorkChange(@PathVariable("id")Integer id){
        Map<String,Object> map = new HashMap<String,Object>();
        int res = workChangeService.deleteWorkChangeById(id);
        if(res<0){
            return Result.fail().error("删除失败");
        }
        return Result.success().addMap(map);
    }


}
